//
//  HZDateHelper.m
//  HZDLHuiXianXia
//
//  Created by zhou Can on 15/10/6.
//  Copyright (c) 2015年 hzdlapple2. All rights reserved.
//

#import "KNDateHelper.h"
@interface KNDateHelper()
@end

@implementation KNDateHelper

+ (instancetype)shareInstance;

{
    static KNDateHelper *helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[self alloc] init];
        
    });
    return helper;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.formatter = [[NSDateFormatter alloc] init];
        self.formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
    }
    return self;
}

- (NSString *)stringFormatWithDate:(NSDate *)date formatString:(NSString *)format
{
    [self.formatter setDateFormat:format];
    NSString *dateString = [self.formatter stringFromDate:date];
    return dateString;
}
+ (long)getShiQuCha
{
    NSDate *date = [NSDate date];
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
    //得到时间偏移量的差值
    long interval = (long)(destinationGMTOffset - sourceGMTOffset);
    
    return interval * 1000;

}
- (NSDate *)dateFormatWithTimeStringSince1970:(NSString *)timeString
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    return date;
}

- (NSDate *)dateFormatWithMillionTimeStringSince1970:(NSString *)millionTimeString
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[millionTimeString doubleValue] / 1000];
    return date;
}

- (NSDate *)dateWithTimeFormatString:(NSString *)timeString {
    [self.formatter setDateFormat:kUTC];
    return [self.formatter dateFromString:timeString];
}

- (NSDate *)dateWithTimeFormat:(NSString *)format timeString:(NSString *)timeString {
    [self.formatter setDateFormat:format];
    return [self.formatter dateFromString:timeString];
}

- (NSInteger)getHourWithDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
    NSInteger hour = [components hour];
    return hour;
}

- (NSInteger)getDayWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitDay) fromDate:date];
    NSInteger hour = [components day];
    return hour;
}

- (NSInteger)getMonthWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitMonth) fromDate:date];
    NSInteger hour = [components month];
    return hour;
}

- (NSInteger)getMinuteWithDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
    NSInteger minute = [components minute];
    return minute;
}
+(NSString *)stringWithInterval:(NSString *)Interval
{
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:Interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kYearAndMothAndDay];
}
+(NSString *)mmStringWithInterval:(NSString *)Interval
{
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:Interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kHourAndMinuteFormat];
}
+(NSString *)utcStringWithInterval:(NSString *)Interval
{
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:Interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kUTC];
}

+ (NSString *)utStringWithInterval:(NSString *)Interval{
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:Interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kUT];
}

+ (NSString *)customStringWithInterval:(NSString *)interval {
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kCustomYearMonthAndDay];
}

+ (NSString *)monthDayHourMinitueStringWithInterval:(NSString *)interval {
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kMonthAndDayAndHourAndMinitue];
}

+ (NSString *)yearAndMonthAndDayStyle2StringWithInterval:(NSString *)interval {
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:kYeaerAndMonthAndDayStyle2];
}

+ (NSString *)stringDateWithInterval:(NSString *)interval formatString:(NSString *)formatString {
    NSDate *date = [[[self class] shareInstance] dateFormatWithTimeStringSince1970:interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:formatString];
}

+ (NSString *)stringDateWithMillionInterval:(NSString *)interval formatString:(NSString *)formatString {
    NSDate *date = [[[self class] shareInstance] dateFormatWithMillionTimeStringSince1970:interval];
    return [[[self class] shareInstance] stringFormatWithDate:date formatString:formatString];
}

+ (NSDate *)dateWithInterval:(NSString *)interval formatString:(NSString *)formatString {
    NSString *dateString = [self stringDateWithInterval:interval formatString:formatString];
    return [[KNDateHelper shareInstance] dateWithTimeFormat:formatString timeString:dateString];
}

@end
